import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
var _excluded = ["secGrpId", "isSelf"];

/**
 * securityGroupService - 安全组service
 * @date: 2019-11-20


 */
import queryString from 'query-string';
import request from 'utils/request';
import { CPAAS_IAM } from 'utils/config';
import { getCurrentOrganizationId, isTenantRoleLevel, parseParameters } from 'utils/utils';
var organizationId = getCurrentOrganizationId();
var organizationRoleLevel = isTenantRoleLevel();
/**
 * 校验数据组编码唯一性
 * @param {object} payload - 编码
 */

export function validateDuplicate(_x) {
  return _validateDuplicate.apply(this, arguments);
}
/**
 * 复制安全组
 * @async
 * @function batchAssignPermissionSets
 * @param {!number} secGrpId - 安全组ID
 * @param {!object} data - 保存参数
 * @returns {object} fetch Promise
 */

function _validateDuplicate() {
  _validateDuplicate = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(payload) {
    var params;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            params = queryString.stringify(payload);
            return _context.abrupt("return", request("".concat(CPAAS_IAM, "/v1/sec-grps/check-duplicate?").concat(params), {
              method: 'POST'
            }));

          case 2:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _validateDuplicate.apply(this, arguments);
}

export function copyPermissionSets(_x2, _x3) {
  return _copyPermissionSets.apply(this, arguments);
}
/**
 * 查询安全组可分配权限的菜单子树（我的安全组）
 * @async
 * @function queryPermissionMenus
 * @param {object} params - 查询参数
 */

function _copyPermissionSets() {
  _copyPermissionSets = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(secGrpId, data) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/sec-grps/").concat(secGrpId, "/copy") : "".concat(CPAAS_IAM, "/v1/sec-grps/").concat(secGrpId, "/copy"), {
              method: 'POST',
              body: data
            }));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _copyPermissionSets.apply(this, arguments);
}

export function queryPermissionMenus(_x4) {
  return _queryPermissionMenus.apply(this, arguments);
}
/**
 * 查询安全组可分配权限的菜单子树(上级、下级创建的安全组)
 * @async
 * @function queryPermissionMenus
 * @param {object} params - 查询参数
 */

function _queryPermissionMenus() {
  _queryPermissionMenus = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    var secGrpId;
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            secGrpId = params.secGrpId;
            return _context3.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/sec-grp-acls/").concat(secGrpId) : "".concat(CPAAS_IAM, "/v1/sec-grp-acls/").concat(secGrpId), {
              method: 'GET',
              query: params
            }));

          case 2:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _queryPermissionMenus.apply(this, arguments);
}

export function queryOthersPermissionMenus(_x5) {
  return _queryOthersPermissionMenus.apply(this, arguments);
}
/**
 * 批量分配权限集至安全组
 * @async
 * @function batchAssignPermissionSets
 * @param {!number} secGrpId - 安全组ID
 * @param {!object} data - 保存参数
 * @returns {object} fetch Promise
 */

function _queryOthersPermissionMenus() {
  _queryOthersPermissionMenus = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(params) {
    var secGrpId, roleId, secGrpSource;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            secGrpId = params.secGrpId, roleId = params.roleId, secGrpSource = params.secGrpSource;
            return _context4.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/sec-grp-acls/").concat(secGrpId, "/assigned") : "".concat(CPAAS_IAM, "/v1/sec-grp-acls/").concat(secGrpId, "/assigned"), {
              method: 'GET',
              query: {
                roleId: roleId,
                secGrpId: secGrpId,
                secGrpSource: secGrpSource
              }
            }));

          case 2:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _queryOthersPermissionMenus.apply(this, arguments);
}

export function batchAssignPermissionSets(_x6, _x7) {
  return _batchAssignPermissionSets.apply(this, arguments);
}
/**
 * 批量取消分配权限集至安全组
 * @async
 * @function batchAssignPermissionSets
 * @param {!number} secGrpId - 安全组ID
 * @param {!object} data - 保存参数
 * @returns {object} fetch Promise
 */

function _batchAssignPermissionSets() {
  _batchAssignPermissionSets = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(secGrpId, data) {
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            return _context5.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/sec-grp-acls/").concat(secGrpId) : "".concat(CPAAS_IAM, "/v1/sec-grp-acls/").concat(secGrpId), {
              method: 'POST',
              body: data
            }));

          case 1:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _batchAssignPermissionSets.apply(this, arguments);
}

export function batchUnassignPermissionSets(_x8, _x9) {
  return _batchUnassignPermissionSets.apply(this, arguments);
}
/**
 * 查询安全组单据类型定义维度分配列表
 * @async
 * @function querySecGrpDimension
 * @param {object} params - 查询参数
 */

function _batchUnassignPermissionSets() {
  _batchUnassignPermissionSets = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(secGrpId, data) {
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            return _context6.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/sec-grp-acls/").concat(secGrpId) : "".concat(CPAAS_IAM, "/v1/sec-grp-acls/").concat(secGrpId), {
              method: 'DELETE',
              body: data
            }));

          case 1:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _batchUnassignPermissionSets.apply(this, arguments);
}

export function querySecGrpDimension(_x10) {
  return _querySecGrpDimension.apply(this, arguments);
} // 查询数据权限-公司tab页的数据

function _querySecGrpDimension() {
  _querySecGrpDimension = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(secGrpId) {
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            return _context7.abrupt("return", request("".concat(CPAAS_IAM, "/v1/sec-grp-dcls/").concat(secGrpId, "/dims/assigned")));

          case 1:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _querySecGrpDimension.apply(this, arguments);
}

export function queryCompanyPermission(_x11) {
  return _queryCompanyPermission.apply(this, arguments);
} // // 查询数据权限-公司tab页的数据(上级或下级角色)
// export async function queryOthersCompanyPermission(params) {
//   const { secGrpId, roleId } = params;
//   const queryParams = queryString.stringify(params);
//   return request(
//     `${CPAAS_IAM}/cpaas/v1/sec-grps/${roleId}/assign-sec-grp/${secGrpId}/dcl-tree?${queryParams}`
//   );
// }

/**
 * 更新安全组新增公司/业务实体/库存组织权限
 */

function _queryCompanyPermission() {
  _queryCompanyPermission = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(params) {
    var secGrpId, isSelf, other;
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            secGrpId = params.secGrpId, isSelf = params.isSelf, other = _objectWithoutProperties(params, _excluded);
            return _context8.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/sec-grp-dcls/").concat(secGrpId, "/authority").concat(isSelf ? '' : '/assigned') : "".concat(CPAAS_IAM, "/v1/sec-grp-dcls/").concat(secGrpId, "/authority").concat(isSelf ? '' : '/assigned'), {
              method: 'GET',
              query: other
            }));

          case 2:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _queryCompanyPermission.apply(this, arguments);
}

export function updateCompanyPermission(_x12) {
  return _updateCompanyPermission.apply(this, arguments);
}
/**
 * 加入全部/取消加入全部
 * @param {objetc} payload - 请求数据
 */

function _updateCompanyPermission() {
  _updateCompanyPermission = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9(payload) {
    var secGrpId, authorityTypeCode;
    return _regeneratorRuntime.wrap(function _callee9$(_context9) {
      while (1) {
        switch (_context9.prev = _context9.next) {
          case 0:
            secGrpId = payload.secGrpId, authorityTypeCode = payload.authorityTypeCode;
            return _context9.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/sec-grp-dcls/").concat(secGrpId, "/authority?authorityTypeCode=").concat(authorityTypeCode) : "".concat(CPAAS_IAM, "/v1/sec-grp-dcls/").concat(secGrpId, "/authority?authorityTypeCode=").concat(authorityTypeCode), {
              method: 'POST',
              body: payload.checkedList
            }));

          case 2:
          case "end":
            return _context9.stop();
        }
      }
    }, _callee9);
  }));
  return _updateCompanyPermission.apply(this, arguments);
}

export function updateIncludeAll(_x13) {
  return _updateIncludeAll.apply(this, arguments);
}
/**
 * 添加数据
 * @param {object} payload - 请求数据
 */

function _updateIncludeAll() {
  _updateIncludeAll = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee10(payload) {
    var secGrpId;
    return _regeneratorRuntime.wrap(function _callee10$(_context10) {
      while (1) {
        switch (_context10.prev = _context10.next) {
          case 0:
            secGrpId = payload.secGrpId;
            return _context10.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/sec-grp-dcls/").concat(secGrpId, "/authority") : "".concat(CPAAS_IAM, "/v1/sec-grp-dcls/").concat(secGrpId, "/authority"), {
              method: 'POST',
              body: payload
            }));

          case 2:
          case "end":
            return _context10.stop();
        }
      }
    }, _callee10);
  }));
  return _updateIncludeAll.apply(this, arguments);
}

export function addData(_x14) {
  return _addData.apply(this, arguments);
}
/**
 * 查询除公司外的数据权限tab列表
 * @param {obejct} params - 查询参数
 */

function _addData() {
  _addData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee11(payload) {
    var secGrpId, data, authorityTypeCode;
    return _regeneratorRuntime.wrap(function _callee11$(_context11) {
      while (1) {
        switch (_context11.prev = _context11.next) {
          case 0:
            secGrpId = payload.secGrpId, data = payload.data, authorityTypeCode = payload.authorityTypeCode;
            return _context11.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/sec-grp-dcls/").concat(secGrpId, "/authority?authorityTypeCode=").concat(authorityTypeCode) : "".concat(CPAAS_IAM, "/v1/sec-grp-dcls/").concat(secGrpId, "/authority?authorityTypeCode=").concat(authorityTypeCode), {
              method: 'POST',
              body: data
            }));

          case 2:
          case "end":
            return _context11.stop();
        }
      }
    }, _callee11);
  }));
  return _addData.apply(this, arguments);
}

export function queryData(_x15) {
  return _queryData.apply(this, arguments);
}

function _queryData() {
  _queryData = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee12(params) {
    var secGrpId, param;
    return _regeneratorRuntime.wrap(function _callee12$(_context12) {
      while (1) {
        switch (_context12.prev = _context12.next) {
          case 0:
            secGrpId = params.secGrpId;
            param = parseParameters(params);
            return _context12.abrupt("return", request(organizationRoleLevel ? "".concat(CPAAS_IAM, "/v1/").concat(organizationId, "/sec-grp-dcls/").concat(secGrpId, "/authority") : "".concat(CPAAS_IAM, "/v1/sec-grp-dcls/").concat(secGrpId, "/authority"), {
              method: 'GET',
              query: _objectSpread({}, param)
            }));

          case 3:
          case "end":
            return _context12.stop();
        }
      }
    }, _callee12);
  }));
  return _queryData.apply(this, arguments);
}